// formatka dodawania dokumentu przychodowego, rozchodowego i ich edycji
unit Unit2;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, sqlite3conn, sqldb, db, FileUtil, LResources, Forms,
  Controls, Graphics, Dialogs, ExtCtrls, StdCtrls, DBGrids, Buttons, EditBtn;

type

  { TForm2 }

  TForm2 = class(TForm)
    BitBtn1: TBitBtn;
    BitBtn2: TBitBtn;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    CheckBox1: TCheckBox;
    CheckBox2: TCheckBox;
    CheckBox3: TCheckBox;
    ds: TDatasource;
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    Edit4: TEdit;
    Edit5: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    query: TSQLQuery;

    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure FormShow(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }

    // indeks edytowanego lub tworzonego dokumentu
    idx_stanowisko : integer;
    idx_klub       : integer;
    idx_czlonek    : integer;


  end;

var
  Form2: TForm2; 

implementation
uses
  Unit3, unit4;
{ TForm2 }

function TestIsFloat(txt :string):boolean;
var
  i       : integer;
  dot_cnt : integer;
begin
  TestIsFloat:=false;
  dot_cnt:=0;
  for i:=1 to length(txt) do
  begin
    if not (txt[i] in ['0'..'9',',']) then exit;
    if txt[i] = ',' then inc(dot_cnt);
  end;
  TestIsFloat:=(dot_cnt <= 1) and (trim(txt) <> '');
end;


// wybór kontrahenta
procedure TForm2.Button1Click(Sender: TObject);
begin
  edit1.Text:=DateTimeToStr(now);
end;

// wybranie stanowiska
procedure TForm2.Button2Click(Sender: TObject);
begin
  idx_stanowisko:=-1;
  form3.Caption:='Wybór stanowiska';
  form3.sql_txt:='select * from stanowisko order by nazwa';
  if form3.ShowModal = mrOK then
  begin
    idx_stanowisko:=form3.idx;
    edit2.Text:=form3.nazwa;
  end;
end;

// wybranie klubu
procedure TForm2.Button3Click(Sender: TObject);
begin
  idx_klub:=-1;
  form3.Caption:='Wybór klubu';
  form3.sql_txt:='select * from klub order by nazwa';
  if form3.ShowModal = mrOK then
  begin
    idx_klub:=form3.idx;
    edit3.Text:=form3.nazwa;
  end;
end;

// wybranie czlonka
procedure TForm2.Button4Click(Sender: TObject);
begin
  idx_czlonek:=-1;
  form3.Caption:='Wybór czlonka';
  form3.sql_txt:='select * from czlonek order by nazwa';
  if form3.ShowModal = mrOK then
  begin
    idx_czlonek:=form3.idx;
    edit4.Text:=form3.nazwa;
  end;
end;

procedure TForm2.FormClose(Sender: TObject; var CloseAction: TCloseAction);
begin
  if TestIsFloat(edit5.Text) then CloseAction:=caHide
  else
    if form2.ModalResult = mrCancel then CloseAction:=caHide
    else
    begin
      ShowMessage('Proszę podac poprawną wagę !');
      form2.ActiveControl:=edit5;
      CloseAction:=caNone;
    end;
end;

procedure TForm2.FormCreate(Sender: TObject);
begin
end;

procedure TForm2.FormShow(Sender: TObject);
begin

//  form2.dbgrid.Columns[4].Visible:=false;
//  form2.dbgrid.Columns[3].Visible:=false;
end;

initialization
  {$I unit2.lrs}

end.

